<?php
error_reporting(0);
ini_set('display_errors', '0');
define( 'UNABLE_TO_CONNECT', 0 );

class MySQL
{

    var $DBhost = "localhost";
    var $DBuser = "root";
    var $DBname = "bamix";
    var $DBpass = "";	

    var $connection;
   
   /****************************************************************************
	Opens a connection to the MySQL DBMS
	* @returns void
   ****************************************************************************/
    function MySQL()
    {      	
        $this->open( );
		
    }


    function MySQL_Old( $DBhost, $DBname, $DBuser, $DBpass )
    {      	
	    $this->DBhost = $DBhost;
        $this->DBuser = $DBuser;
        $this->DBpass = $DBpass;
        $this->DBname = $DBname;
        
        $this->open( );
    }


   /****************************************************************************
	Opens a connection to the MySQL DBMS
	* @returns void
   ****************************************************************************/
    function open( )
    {      
	    /* attempt connection to MySQL DBMS */
	    $this->connection = @mysql_connect( $this->DBhost, $this->DBuser, $this->DBpass );

        /* if connection could not be established, die off */
        $this->connection == UNABLE_TO_CONNECT ? die( $this->mysql_report( 'Unable To Connect Database' ) ) : '';

        /* attempt to select the database and use it */
        $SelectDB = @mysql_select_db( $this->DBname, $this->connection );
		mysql_query("SET NAMES 'utf8'");

        /* if the link to the database could not be made, die off */
        $SelectDB ? '' : die( $this->mysql_report( 'Unable to connect to database using: <b>' . $this->DBname . '</b>' ) );
    }
    
    
   /*********************************************************************************
	Close MySQL server connection
	* @returns void
   **********************************************************************************/
	function close( )
    {
        @mysql_close( $this->connection ) or die( mysql_error( ) );
    }


   /****************************************************************************
	Selects rows from the database with the given SQL Statement
	* @param $query string. An SQL statement
	* @returns int
   ****************************************************************************/
    function select( $query )
    {
        $sql = @mysql_query( $query, $this->connection ) or die( $this->mysql_report( $query ) );

		return $sql;
    }


   /****************************************************************************
	Fetch and return an array of results from the database, or FALSE if null.
	* @param $query_id int.
	* @returns mixed[]
   ****************************************************************************/
    function fetch( $query_id )
	{
		if( $this->row[$query_id] = @mysql_fetch_array( $query_id, MYSQL_ASSOC ) )
		{
			return $this->row[$query_id];
		}
	}
	
   /****************************************************************************
	Return number of rows retrieved, or FALSE if null.
	* @param $query_id int. Resource identifier
	* @returns int
   ****************************************************************************/
    function numrows( $query_id = 0 )
    {
        return $query_id ? @mysql_num_rows( $query_id ) : false;
    }
    
    
   /****************************************************************************
	Return number of rows retrieved, or FALSE if null.
	* @param $query_id int. Resource identifier
	* @returns int
   ****************************************************************************/
    function numfields( $query_id = 0 )
    {
        return $query_id ? @mysql_num_fields( $query_id ) : false;
    }


   /****************************************************************************
	Inserts rows and returns the last insert id generated by the database.
	* @param $query string. An SQL statement
	* @returns int
   ****************************************************************************/
	function insert( $query )
    {
		$this->select( $query );

		/* Get last insert ID */
        $id = @mysql_insert_id( $this->connection );

		return $id;
	}


   /****************************************************************************
	Updates rows and returns the number of rows updated.
	* @param $insertQuery string. An SQL statement
	* @returns int
   ****************************************************************************/
    function update( $query )
    {
        $this->select( $query );

        /* Get number of affected rows */
        $NumberOfRows = @mysql_affected_rows( $this->connection );

        return $NumberOfRows;
    }

   
   /****************************************************************************
	Delete rows and returns the number of rows deleted.
	* @param $insertQuery string. An SQL statement
	* @returns int
   ****************************************************************************/
    function delete( $query )
    {
   	    $this->select( $query );

        /* Get number of affected rows */
        $NumberOfRows = @mysql_affected_rows( $this->connection );

	    return $NumberOfRows;
    }


   /****************************************************************************
	Returns the contents of one cell from a MySQL result set, or FALSE if null.
	* @param $query_id int. Resource identifier
	* @returns int
   ****************************************************************************/
    function resultData( $query_id )
    {
        return ( $query_id ) ? @mysql_result( $query_id, 0 ) : false;
    }
	   
    
   /****************************************************************************
	Error Reporting
	* @returns void
   ****************************************************************************/
    function mysql_report( $statement )
    { 	
        $report = '<pre><strong><u>Database Error Reporting</u></strong>' . "\n" .
                  '<strong>Query:</strong> ' . $statement . "\n" .
                  '<strong>MySQL Error Message:</strong> ' . mysql_error( ) . "\n" .
                  '<strong>Date Occured:</strong> ' . date( 'l dS of F Y h:i:s A' ) . "\n" .	
				  '</pre>';

   		echo $report;
    }
}
?>
